﻿using System.ComponentModel.DataAnnotations;

namespace FileClient.Core.Configuration;

public class FileServerOption
{
    public static string FileServerSetting = "FileServerSetting";

    public FileServerOption()
    {
        UseLibuv = true;
        SoBacklog = 1024;
        ConnectTimeout = 1000;
        FilePieceSize = 1024 * 100;
        ProgressStep = 2;
    }

    [Required]
    [RegularExpression( @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]):\d{0,5}$",ErrorMessage = "文件服务地址不正确")]
    public string FileServerEndpoint { get; set; }
    public bool UseLibuv { get; set; }
    public int SoBacklog { get; set; }
    public int ConnectTimeout { get; set; }

    public int ProgressStep { get; set; }

    public int FilePieceSize { get; set; }

    public string Ip
    {
        get
        {
            if (string.IsNullOrEmpty(FileServerEndpoint))
                throw new Exception("文件服务地址不允许为空");
            return FileServerEndpoint.Split(":")[0];
        }
    }

    public int Port
    {
        get
        {
            if (string.IsNullOrEmpty(FileServerEndpoint))
                throw new Exception("文件服务地址不允许为空");
            return Int32.Parse(FileServerEndpoint.Split(":")[1]);
        }
        
    }
}